package com.lebang.medicalInquiry.service;

import com.lebang.medicalInquiry.exception.BusinessException;
import com.lebang.medicalInquiry.mapper.*;
import com.lebang.medicalInquiry.model.po.Organization;
import com.lebang.medicalInquiry.model.po.Physician;
import com.lebang.medicalInquiry.model.po.PhysicianCase;
import com.lebang.medicalInquiry.model.po.UserPhysicianAttention;
import com.lebang.medicalInquiry.model.vo.OrganizationVo;
import com.lebang.medicalInquiry.model.vo.PhysicianInfoVo;
import com.lebang.medicalInquiry.model.vo.SubjectClassifyVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class PhysicianService {

    @Resource
    private PhysicianMapper physicianMapper;

    @Resource
    private PhysicianCaseMapper physicianCaseMapper;

    @Resource
    private OrganizationMapper organizationMapper;

    @Resource
    private SubjectClassifyMapper subjectClassifyMapper;

    @Resource
    private PhysicianOrganizationRelevanceMapper physicianOrganizationRelevanceMapper;

    @Resource
    private UserPhysicianAttentionMapper userPhysicianAttentionMapper;

    public List<Physician> getPhysicianList() {
        return physicianMapper.getAllEnable();
    }

    public PhysicianInfoVo getPhysicianInfo(Integer physicianId) {
        PhysicianInfoVo physician = physicianMapper.getInfoById(physicianId);
        if (physician == null) {
            throw new BusinessException("当前医师不存在");
        }
        //TODO 现在暂时只有一个机构 所以只有一名负责医生的客服 所以直接 返回负责医生的客服人员
        String uuid = physicianOrganizationRelevanceMapper.getOperatorByPhysicianId(physicianId);
        physician.setOperatorUuid(uuid);
        return physician;
    }

    public List<PhysicianCase> getPhysicianCaseList(Integer physicianId, Integer type) {
        return physicianCaseMapper.getByPhysicianId(physicianId, type);
    }

    public List<OrganizationVo> getOrganizationList(Integer physicianId) {
        return organizationMapper.getOrganizationListByPId(physicianId);
    }

    public Organization getOrganizationInfo(Integer organizationId) {
        return organizationMapper.selectByPrimaryKey(organizationId);
    }

    /**
     * 获取所有可用医师的分类标签
     *
     * @return
     */
    public List<SubjectClassifyVo> getSubjectClassifyList(Integer isPhysician) {
        List<SubjectClassifyVo> listVos = new ArrayList<>();
        List<SubjectClassifyVo> physicantClassify = subjectClassifyMapper.getPhysicantClassify(isPhysician);
        if (physicantClassify.size() == 0) {
            return listVos;
        }
        Map<String, List<SubjectClassifyVo>> collect = physicantClassify.stream().collect(Collectors.groupingBy(SubjectClassifyVo::getParentName));
        collect.forEach((k, v) -> {
            SubjectClassifyVo subjectClassifyVo = new SubjectClassifyVo();
            subjectClassifyVo.setName(k);
            subjectClassifyVo.setSonClassifyListVos(v);
            listVos.add(subjectClassifyVo);
        });

        return listVos;
    }

    /**
     * 关注或者取消关注
     *
     * @param uuid
     * @param physicianId
     * @param attentionState
     */
    public void attentionPhysician(String uuid, Integer physicianId, Integer attentionState) {

        if (attentionState == 1) {
            Integer count = userPhysicianAttentionMapper.countByUuidAndPhysicianId(uuid, physicianId);
            if (count > 0) {
                throw new BusinessException("已关注");
            }
            UserPhysicianAttention userPhysicianAttention = new UserPhysicianAttention();
            userPhysicianAttention.setPhysicianId(physicianId);
            userPhysicianAttention.setUuid(uuid);
            userPhysicianAttention.setState(1);
            userPhysicianAttention.setCreateTime(LocalDateTime.now());
            userPhysicianAttention.setUpdateTime(LocalDateTime.now());
            userPhysicianAttention.setHasDeleted(0);
            userPhysicianAttentionMapper.insertSelective(userPhysicianAttention);
        } else {
            userPhysicianAttentionMapper.deleteByUuidAndPhysicianId(uuid, physicianId);
        }
    }

    public Physician getByLiveRoomId(String liveRoomId) {
        return physicianMapper.getByLiveRoomId(liveRoomId);
    }

    public Map<Integer, Physician> getHeadImg(List<Integer> ids) {
        List<Physician> physicians = physicianMapper.getHeadImg(ids);
        return physicians.stream().collect(Collectors.toMap(Physician::getId, Function.identity()));
    }
}
